#!/usr/bin/env python
#-*-coding:utf-8-*-
#通过给定的值,计算聚类中间文件中,大于平均值,小于平均值的数量
import sys
import os
import re
print "输入格式  python week_uv_probability.py 聚类结果文件  聚类中间文件  按照聚类结果文件给出的各个聚类结果的平均值"


if len(sys.argv) < 4:
    print "argv's length must larger than 4"
    sys.exit()
cluster_result_file_path = sys.argv[1]
cluster_mid_file_path = sys.argv[2]
if not os.path.exists(cluster_result_file_path):
    print 'file: %s does not exist' % cluster_result_file_path
    sys.exit()
elif not os.path.exists(cluster_mid_file_path):
    print 'file: %s does not exist' % cluster_mid_file_path
    sys.exit()
else:
    #将平均值放入数组
    cluster_avg_value = []
    for arg in sys.argv[3:]:
        cluster_avg_value.append(float(arg))
    
    cluster_user_distribution = {}
    
    cluster_result_file = open(cluster_result_file_path)
    cluster_num_pattern = "Cluster Number = (\d+)"
    cluster_num_re = re.compile(cluster_num_pattern)
    map_k_id_v_clusterId = {}
    for line in cluster_result_file:
        cluster_num_m = cluster_num_re.match(line)
        if cluster_num_m is not None:
            infos = cluster_num_m.groups()
            cluster_num_value = int(infos[0])
            #构建根据分类id组成的大于平均值,小于平均值的数组
            for i in range(0,cluster_num_value):
                user_distribution = [0,0]
                cluster_user_distribution[i] = user_distribution
            i = 0
            cluster_num_id_pattern = ""
            while i < cluster_num_value:
                if cluster_num_id_pattern == "":
                    cluster_num_id_pattern = "Cluster Id = " + str(i) + " Cluster Center is:"
                    cluster_num_id_re = re.compile(cluster_num_id_pattern)                    
                cluster_num_id_m = cluster_num_id_re.match(line)
                if cluster_num_id_m is not None:
                    for k in range(1,3):
                        #读取文件的下一行
                        cluster_result_file.next()
                    ids_line = cluster_result_file.next()
                    ids = ids_line.rstrip().lstrip().split(" ")
                    for id_num in ids:
                        #将每一类及对应的ids放入map
                        map_k_id_v_clusterId[id_num] = i        
                        i += 1
                        cluster_num_id_pattern = ""
                else:
                    line = cluster_result_file.next()
            break
    cluster_result_file.close()
    print "读取结果文件完毕,开始读取中间文件"
    cluster_mid_file = open(cluster_mid_file_path)
    for line in cluster_mid_file:
        columns = line.rstrip().lstrip().split(" ")
        column_name = columns[0]
        week_avg_value = float(columns[1])
        cluster_id_value = map_k_id_v_clusterId[column_name]
        user_distribution = cluster_user_distribution[i]
        if week_avg_value >= cluster_avg_value[i]:
            user_distribution[0] += 1
        else:
            user_distribution[1] += 1
    cluster_mid_file.close()
    #打印出概率
    